import { Vue } from '@/main.js';
let dialogContainer = document.body;
export default function showConfirm(title, comp, data, width, height) {
  let sourceData = JSON.stringify(data);
  let vm = null;
  let exec = new Promise((resolve, reject) => {
    let confirmOptions = {
      data: function () {
        return {
          title: title,
          dialogVisible: true,
          height: height,
          width: width,
          sourceData: sourceData,
        };
      },
      methods: {
        clickBtn(obj) {
          resolve(obj);
        },
        handleCancel(data) {
          this.dialogVisible = false;
          this.clickBtn({
            type: 'cancel',
            data: data,
          });
        },
        handleOk(data) {
          this.dialogVisible = false;
          this.clickBtn({
            type: 'ok',
            data: data,
          });
          dialogClose(vm);
        },
      },
      template: [
        '<div id="commonDialog" v-if="dialogVisible" class="fxrow fxcc" style="position:absolute;width:100%;height:100%;top:0;left:0;z-index:4;">',
        '<div :style="\'height:' +
        height +
        ';width:\' + width + \';background-color:#fff;z-index:1000;\'" class="fxcol fxcc">',
        '<div style="width:100%;height:43px;line-height:43px;color:#fff;background:#409eff;font-size:15px;text-align:left;padding-left:15px;" >{{title}}<span style="float:right;margin-right:10px" @click="handleCancel"><i class="el-icon-close"></i></span></div>',
        '<div class="content-rel" style="padding:10px 10px;overflow:auto;">',
        '<comp @ok="handleOk" :sourceData=\'' +
        sourceData +
        '\' @cancel="handleCancel"></comp>',
        '</div></div>',
        '<div style="position:fixed;left:0;top:0;width:100%;height:100%;opacity:0.5;background:#000;"></div>',
        '</div>',
      ].join(''),
      components: {
        comp,
      },
    };
    let creator = Vue.extend(confirmOptions);
    vm = new creator().$mount();
    dialogContainer.appendChild(vm.$el);
  });
  return {
    vm,
    exec,
  };
}
//删除组件vm（从文本流中移除，并在vue中销毁）
function dialogClose(vm) {
  dialogContainer.removeChild(vm.$el);
  vm.$destroy();
}
